package model.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import tools.excel.tool;
import dom.domain.Examplace;
import dom.domain.ExamplaceDAO;
import dom.domain.Mobileadmistrator;
import dom.domain.MobileadmistratorDAO;

public class MAdminService {
	private Map<String,String> map;
	private MobileadmistratorDAO mAdminDao;
	private ExamplaceDAO epDao;
	
	public void setEpDao(ExamplaceDAO epDao) {
		this.epDao = epDao;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public void setmAdminDao(MobileadmistratorDAO mAdminDao) {
		this.mAdminDao = mAdminDao;
	}
	
	public String add(String mAdminInfo)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jInfo = JSONObject.fromObject(mAdminInfo);
			if(!jInfo.containsKey("adminId"))
			{
				map.put("flag","false");
				map.put("Info", "adminId为空");
				return JSONObject.fromObject(map).toString();
			}
			Mobileadmistrator temp = mAdminDao.findById(jInfo.getString("adminId"));
			if(temp!=null){
				map.put("flag", "false");
				map.put("Info", "mAdminId已存在");
				return JSONObject.fromObject(map).toString();
			}
			if(!jInfo.containsKey("adminPsd"))
			{
				map.put("flag", "false");
				map.put("Info","adminPsd为空");
				return JSONObject.fromObject(map).toString();
			}
			if(!jInfo.containsKey("epname"))
			{
				map.put("flag", "false");
				map.put("Info", "epname为空");
				return JSONObject.fromObject(map).toString();
			}
			temp = new Mobileadmistrator();
			temp.setAdminId(jInfo.getString("adminId"));
			temp.setAdminPsd(jInfo.getString("adminPsd"));
			temp.setEpname(jInfo.getString("epname"));
			mAdminDao.save(temp);
			map.put("flag", "true");
			map.put("Info", "添加成功");
		}catch(JSONException e)
		{
			map.put("flag", "false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String allocateAll()
	{
		List list = epDao.findAll();
		for(int i=0;i<list.size();i++)
		{
			Examplace ep = (Examplace) list.get(i);
			Mobileadmistrator temp = new Mobileadmistrator();
			temp.setAdminId(tool.generateShortUuid());
			temp.setAdminPsd(tool.getPassword());
			temp.setEpname(ep.getEpname());
			mAdminDao.save(temp);
		}
		map.put("flag", "true");
		map.put("Info","分配成功");
		return JSONObject.fromObject(map).toString();
	}
	public String  deleteAll(String adminIds)
	{
		String[] admins = adminIds.split(",");
		StringBuilder Info = new StringBuilder();
		for(int i = 0; i < admins.length; i++)
		{
			JSONObject result = JSONObject.fromObject(delete(admins[i]));
			if(result.getString("flag").equals("false"))
			{
				Info.append(result.getString("Info"));
			}
		
		}
		map.put("flag","true");
		map.put("Info",Info.toString());
		return JSONObject.fromObject(map).toString();
	}

	public String delete(String adminId)
	{
		map = new HashMap<String,String>();
		if(adminId==null)
		{

			map.put("flag", "false");
			map.put("Info", "adminId为空");
			return JSONObject.fromObject(map).toString();
		
		}
		Mobileadmistrator temp = mAdminDao.findById(adminId);
		if(temp==null)
		{
			map.put("flag", "false");
			map.put("Info", "adminId不存在");
//			return JSONObject.fromObject(map).toString();
		}
		else
		{
			mAdminDao.delete(temp);
			map.put("flag", "true");
			map.put("Info","删除成功");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String getList(int pageNum)
	{
		
		map = new HashMap<String,String>();
		List list = mAdminDao.getList(pageNum, 10);
		JSONArray array = new JSONArray();
		for(int i= 0;i<list.size();i++)
		{
			Mobileadmistrator temp = (Mobileadmistrator)list.get(i);
			JSONObject jInfo = JSONObject.fromObject(temp.toString());
//			Examplace ep = epDao.findById(Integer.parseInt(temp.getEpname()));
//			jInfo.put("", value)
			array.add(JSONObject.fromObject(temp.toString()));
		}
		if(array.size()==0)
		{
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info", "success");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
		
	}
	public String modify(String adminInfo)
	{
		map = new HashMap<String,String>();
		try
		{
			JSONObject jInfo = JSONObject.fromObject(adminInfo);
			if(!jInfo.containsKey("adminId"))
			{
				map.put("flag","false");
				map.put("Info", "adminId为空");
				return JSONObject.fromObject(map).toString();
			}
			Mobileadmistrator temp = mAdminDao.findById(jInfo.getString("adminId"));
			if(temp==null){
				map.put("flag", "false");
				map.put("Info", "mAdminId不存在");
				return JSONObject.fromObject(map).toString();
			}
			if(!jInfo.containsKey("adminPsd"))
			{
				map.put("flag", "false");
				map.put("Info","adminPsd为空");
				return JSONObject.fromObject(map).toString();
			}
			if(!jInfo.containsKey("epname"))
			{
				map.put("flag", "false");
				map.put("Info", "epname为空");
				return JSONObject.fromObject(map).toString();
			}
			temp = new Mobileadmistrator();
			temp.setAdminId(jInfo.getString("adminId"));
			temp.setAdminPsd(jInfo.getString("adminPsd"));
			temp.setEpname(jInfo.getString("epname"));
			mAdminDao.attachDirty(temp);
			map.put("flag", "true");
			map.put("Info", "修改成功");
		}catch(JSONException e)
		{
			map.put("flag","false");
			map.put("Info",e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String query(String adminId)
	{
		map = new HashMap<String,String>();
		if(adminId==null){
			map.put("flag", "false");
			map.put("Info", "adminId为空");
			return JSONObject.fromObject(map).toString();
		}
		Mobileadmistrator temp = mAdminDao.findById(adminId);
		if(temp==null)
		{
			map.put("flag", "false");
			map.put("Info", "adminId不存在");
//			return JSONObject.fromObject(map).toString();
		}
		else
		{
			map.put("flag","true");
			map.put("Info","查询成功");
			map.put("adminInfo",JSONObject.fromObject(temp.toString()).toString());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String queryByEpname(String epname)
	{
		map = new HashMap<String,String>();
		if(epname==null)
		{
			map.put("flag", "true");
			map.put("Info", "epname为空");
			return JSONObject.fromObject(map).toString();
		}
		JSONArray array = new JSONArray();
		List list = mAdminDao.query(epname);
		for(int i=0;i<list.size();i++)
		{
			Mobileadmistrator mAdmin = (Mobileadmistrator) list.get(i);
			array.add(JSONObject.fromObject(mAdmin.toString()));
		}
		if(array.size()==0)
		{
			map.put("flag","false");
			map.put("Info", "未查到任何数据");
		}
		else
		{
		map.put("flag", "true");
		map.put("Info", "success");
		map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}

}
